package com.distributer.message.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.distributer.message.model.Message;
import com.distributer.message.service.MessageService;

@RestController
@RequestMapping("/message")
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	@RequestMapping("/reserve/message")
	public String reserveSendMessage(Message message){
		return messageService.reserveSendMessage(message);
	}
	
	@RequestMapping("/confirm/send/message")
	public void confirmSendMessage(String messageSquece){
		messageService.confirmSendMessage(messageSquece);
	}
	
	@RequestMapping("/send/message")
	public void sendMessage(Message message){
		messageService.sendMessage(message);
	}
	
	@RequestMapping("/retry/message")
	public void retryMessage(){
		
	}
	
	@RequestMapping("/confrim/message")
	public void confirmMessage(){
		
	}
	
	@RequestMapping("/ack/message")
	public void ackMessage(){
		
	}
	
	@RequestMapping("/test/strom/kafka/message/{count}")
	public void stromKafkaTest(@PathVariable(required=true,name="count") Integer count){
		int orderNoProfix = 1000;
		List<Map<String, String>> productDatas = new ArrayList<Map<String,String>>();
		Map<String, String> productData1 = new HashMap<String, String>();
		productData1.put("productCode", "productCode1");
		productData1.put("orderPremium", "100");
		Map<String, String> productData2 = new HashMap<String, String>();
		productData2.put("productCode", "productCode2");
		productData2.put("orderPremium", "200");
		Map<String, String> productData3 = new HashMap<String, String>();
		productData3.put("productCode", "productCode3");
		productData3.put("orderPremium", "300");
		productDatas.add(productData1);
		productDatas.add(productData2);
		productDatas.add(productData3);
		String[] orderStatus = {"10","20","30"};
		for (int i = 1; i <= count; i++) {
			Map<String, String> data = new HashMap<String, String>();
			data.put("orderNo", String.valueOf(orderNoProfix+i));
			Map<String, String> productInfo = getProductInfo(productDatas);
			data.put("productCode", productInfo.get("productCode"));
			data.put("orderStatus", getRandomOrderStatus(orderStatus));
			data.put("orderPremium", productInfo.get("orderPremium"));
			Message message = new Message();
			message.setNotifyData(JSONObject.toJSONString(data));
			message.setNotifyTopic("order_statistics_data");
			messageService.sendMessage(message);
		}
	}

	private Map<String, String> getProductInfo(List<Map<String, String>> productDatas) {
		Random random = new Random();
		int randomData = random.nextInt(10000);
		int index = (int) (randomData % 3);
		return productDatas.get(index);
	}
	
	private String getRandomOrderStatus(String[] orderStatus) {
		Random random = new Random();
		int randomData = random.nextInt(10000);
		int index = (int) (randomData % 3);
		return orderStatus[index];
	}
}
